﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CarbonExchange.Bll
{
    public class EmailActivationCode : ItemBase
    {
        internal EmailActivationCode()
        {
        }

        public EmailActivationCode(int userId, string emailAddress)
        {
            this.UserId = userId;
            this.EmailAddress = emailAddress;
            SetActivationCode();
        }

        public EmailActivationCode(User user, string emailAddress)
        {
            this.User = User;
            this.EmailAddress = emailAddress;
            SetActivationCode();
        }

        public int Id { get; internal set; }
        public int UserId { get; internal set; }
        public string Code { get; internal set; }
        public string EmailAddress { get; internal set; }
        public bool Activated { get; set; }
        public DateTime Created { get; internal set; }
        internal LinqUtilities.LazyItem<User, Dal.User> _User;
        public User User
        {
            get
            {
                if (_User != null)
                    return _User.Item;
                else
                    return null;
            }
            set
            {
                if (value != null)
                {
                    _User.Item = value;
                    UserId = value.Id;
                }
            }
        }

        private void SetActivationCode()
        {
            char[] chars = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
            Random rnd = new Random();
            Code = string.Format("{0}{1}{2}{3}", chars[rnd.Next(0, chars.Length)], rnd.Next(0, 1000), chars[rnd.Next(0, chars.Length)], rnd.Next(0, 1000));
            if (Code.Length > 10)
                Code = Code.Substring(0, 10);

        }
    }
}
